package com.sywb.chuangyebao.contract;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.BusinessBean;
import com.sywb.chuangyebao.utils.GlideUtils;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.view.fragment.ConsultationFragment;
import com.sywb.chuangyebao.view.fragment.WebFragment;
import com.sywb.chuangyebao.widget.AutoHeightViewPager;
import com.sywb.chuangyebao.widget.GridSpacingItemDecoration;
import com.sywb.chuangyebao.widget.XTabLayout;

import org.bining.footstone.adapter.BaseRecyclerAdapter;
import org.bining.footstone.adapter.ViewHolderHelper;
import org.bining.footstone.rxjava.rxbus.RxBus;
import org.bining.footstone.widget.SlideView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pxb on 2018/6/13.
 */

public interface BusinessDetailsContract {

    interface BusinessDetailsView extends BaseRecyclerContract.View {
        FragmentManager getUserFragmentManager();

        RecyclerView getRecycler();

        AutoHeightViewPager getViewPager();

        SlideView getSlideView();

        String getPiD();

        CoordinatorLayout getC1Index();

        XTabLayout getTabLayout();

        RelativeLayout getLayoutRl();

        TextView getLayoutTv();
    }

    class BusinessDetailsPresenter extends BaseRecyclerContract.Presenter<BusinessDetailsView> {
        public AutoHeightViewPager viewPager;
        private ChannelPagerFragmentAdapter pagerFragmentAdapter = null;
        public List<Fragment> fragments = null;
        private List<String> channels;
        private SlideView slideView;
        private GuessLikeAdapter guessLikeAdapter = null;
        private RecyclerView recyclerView;
        private String projectId;
        public int pageNo = 1;
        private CoordinatorLayout ct1Index;
        private AutoHeightViewPager vpIndex;
        private XTabLayout tabLayout;
        private RelativeLayout layoutRl;
        private TextView layoutTv;

        @Override
        public void onStart() {
            super.onStart();
            fragments = new ArrayList<>();
            channels = new ArrayList<>();
            slideView = mView.getSlideView();
            projectId = mView.getPiD();
            ct1Index = mView.getC1Index();
            vpIndex = mView.getViewPager();
            tabLayout = mView.getTabLayout();
            layoutRl = mView.getLayoutRl();
            layoutTv = mView.getLayoutTv();
            guessLikeAdapter = new GuessLikeAdapter(mContext);
            recyclerView = mView.getRecycler();
            GridSpacingItemDecoration recyclerDivider = new GridSpacingItemDecoration(mActivity.getResources().getDimensionPixelSize(R.dimen.dp_15), mActivity.getResources().getDimensionPixelSize(R.dimen.dp_4), false);
            recyclerView.addItemDecoration(recyclerDivider);
            recyclerView.setLayoutManager(new GridLayoutManager(mContext, 2));
            List<Integer> images = new ArrayList<>();
            images.add(R.drawable.provider_bg);
            images.add(R.drawable.tutor_bg);
            images.add(R.drawable.project_number_bg);
            images.add(R.drawable.user_bg);
            slideView.setImageViews(images);
            getBusinessDetails();
        }

        /**
         * 初始化View
         *
         * @param viewPager
         * @param xTabLayout
         */
        public void initTabView(AutoHeightViewPager viewPager, XTabLayout xTabLayout, BusinessBean businessBean) {
            this.viewPager = viewPager;
            initThreeView(mContext.getResources().getStringArray(R.array.business_details_title), businessBean);
            viewPager.setCurrentItem(0);
            xTabLayout.setupWithViewPager(viewPager);
        }

        //获取项目详情
        private void getBusinessDetails() {
            HttpUtils.getBusinessDetails(projectId, pageNo, Constants.PAGESIZE, new HttpCallback<BusinessBean>() {
                @Override
                public void onStart() {
                    super.onStart();
                    ct1Index.setVisibility(View.INVISIBLE);
                    mView.showProgress();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    mView.hideProgress();
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                }

                @Override
                public void onSuccess(BusinessBean businessBean) {
                    ct1Index.setVisibility(View.VISIBLE);
                    initTabView(vpIndex, tabLayout, businessBean);
                    if (businessBean != null && businessBean.getRecommend() != null && businessBean.getRecommend().size() > 0) {
                        layoutRl.setVisibility(View.VISIBLE);
                        layoutTv.setText(R.string.guess_user_like);
                        guessLikeAdapter.setDatas(businessBean.getRecommend());
                        recyclerView.setAdapter(guessLikeAdapter);
                    } else {
                        layoutRl.setVisibility(View.INVISIBLE);
                    }
                    RxBus.get().post(Constants.POST_BUSINESS_DETAILS, businessBean);
                }
            });
        }

        private String isNull(String str) {
            return str == null ? "" : str;
        }

        private void initThreeView(String[] titles, BusinessBean businessBean) {
            Fragment fragment = null;
            for (int i = 0; i < titles.length; i++) {
                channels.add(titles[i]);
                switch (i) {
                    case 0:
                        fragment = WebFragment.newInstance(3, isNull(businessBean.getPoster_content()));
                        break;
                    case 1:
                        fragment = WebFragment.newInstance(3, isNull(businessBean.getIntroduce()));
                        break;
                    case 2:
                        fragment = WebFragment.newInstance(3, isNull(businessBean.getMarket_analysis()));
                        break;
                    case 3:
                        fragment = ConsultationFragment.newInstance(businessBean);
                        break;
                }
                fragments.add(fragment);
            }
            pagerFragmentAdapter = new ChannelPagerFragmentAdapter(mView.getUserFragmentManager(), fragments, channels);
            viewPager.setAdapter(pagerFragmentAdapter);
            viewPager.setCurrentItem(0);
        }

        class ChannelPagerFragmentAdapter extends FragmentStatePagerAdapter {
            private List<Fragment> fragments;
            private List<String> channels;

            public ChannelPagerFragmentAdapter(FragmentManager fragmentManager, List<Fragment> fragments, List<String> channels) {
                super(fragmentManager);
                this.fragments = fragments;
                this.channels = channels;
            }

            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return channels.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return channels == null ? "" : channels.get(position);
            }

            @Override
            public int getItemPosition(Object object) {
                return POSITION_NONE;
            }
        }

        //猜你喜欢
        class GuessLikeAdapter extends BaseRecyclerAdapter<BusinessBean.RecommendBean> {

            public GuessLikeAdapter(Context context) {
                super(context, R.layout.item_guess_like);
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int i, BusinessBean.RecommendBean recommendBean) {
                ImageView imageView = (ImageView) viewHolderHelper.getView(R.id.iv_img);
                if (recommendBean.getLogo() != null) {
                    GlideUtils.getNormalIamgeToUrl((Activity) mContext, recommendBean.getLogo(), imageView);
                }
                viewHolderHelper.setText(R.id.tv_title, recommendBean.getProject_name());
                viewHolderHelper.setText(R.id.tv_amount_num, recommendBean.getInvestment());
            }
        }

    }
}
